package com.xique.park.bean.transform.app;

import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.bean.BeanUtils;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.User;
import com.xique.park.bean.request.app.ParkCarAddRequest;
import com.xique.park.bean.request.app.ParkCarEditRequest;
import com.xique.park.bean.request.app.ParkCarFeeRequest;
import com.xique.park.bean.request.web.car.ParkVisitorCarAddRequest;
import com.xique.park.bean.vo.app.ParkCarEditVo;
import com.xique.park.bean.vo.app.ParkCarVo;
import com.xique.park.bean.vo.web.car.ParkCarDetailVo;

/**
 * 车辆前端对象转化类
 *
 * @author liuxueli
 * @create 2021/12/13
 */
public class ParkCarTransform {

    /**
     * 转化为车辆列表前端显示实体
     */
    public static ParkCarVo transformParkCarVo(ParkCar parkCar, User user) {
        ParkCarVo parkCarVo = new ParkCarVo();
        parkCarVo.setBeginDate(null != parkCar.getBeginDate() ? parkCar.getBeginDate() : null);
        parkCarVo.setEndDate(null != parkCar.getEndDate() ? parkCar.getEndDate() : null);
        parkCarVo.setCarCnt(StringUtils.isNotEmpty(parkCar.getBindPlates()) ? parkCar.getBindPlates().split(",").length : 0);
        parkCarVo.setCarId(parkCar.getId());
        parkCarVo.setMngType(parkCar.getMngType());
        parkCarVo.setMngTypeId(parkCar.getMngTypeId());
        parkCarVo.setPlates(parkCar.getBindPlates());
        parkCarVo.setSpaceCnt(parkCar.getSpaces());
        parkCarVo.setSeatNo(parkCar.getSeatNo());
        parkCarVo.setMayFee(parkCar.getMayFee());
        parkCarVo.setActualFee(parkCar.getActualFee());
        parkCarVo.setPayType(parkCar.getPayType());
        parkCarVo.setForeverStatus(parkCar.getForeverStatus());
        parkCarVo.setIsGarageSpace(parkCar.getIsGarageSpace());
        // 判断是否有车主
        if (null != user) {
            parkCarVo.setUserId(parkCar.getParkUserId());
            parkCarVo.setUserName(user.getUserName());
            parkCarVo.setPhone(user.getPhone());
            parkCarVo.setAddress(user.getAddress());
        }
        return parkCarVo;
    }

    /**
     * 转化为车辆编辑前端显示实体
     */
    public static ParkCarEditVo transformParkCarEditVo(ParkCar parkCar, User user) {
        ParkCarVo parkCarVo = transformParkCarVo(parkCar, user);
        ParkCarEditVo parkCarEditVo = new ParkCarEditVo();
        BeanUtils.copyProperties(parkCarVo, parkCarEditVo);
        parkCarEditVo.setRemark(parkCar.getRemark());
        parkCarEditVo.setSeatNo(parkCar.getSeatNo());
        parkCarEditVo.setStatus(parkCar.getStatus());
        if (user != null){
            parkCarEditVo.setUserRemark(user.getRemark());
            parkCarEditVo.setTelephone(user.getTelephone());
        }

        return parkCarEditVo;
    }

    /**
     * 转化为车辆详情前端显示实体
     */
    public static ParkCarDetailVo transformParkCarDetailVo(ParkCar parkCar, User user) {
        ParkCarVo parkCarVo = transformParkCarVo(parkCar, user);
        ParkCarDetailVo parkCarDetailVo = new ParkCarDetailVo();
        BeanUtils.copyProperties(parkCarVo, parkCarDetailVo);
        parkCarDetailVo.setRemark(parkCar.getRemark());
        parkCarDetailVo.setSeatNo(parkCar.getSeatNo());
        parkCarDetailVo.setStatus(parkCar.getStatus());
        if (user != null){
            parkCarDetailVo.setUserRemark(user.getRemark());
            parkCarDetailVo.setTelephone(user.getTelephone());
        }
        parkCarDetailVo.setUpdateUserId(parkCar.getUpdateUser());
        parkCarDetailVo.setUpdateTime(parkCar.getUpdateTime());
        parkCarDetailVo.setBalance(parkCar.getBalance());
        parkCarDetailVo.setCreateTime(parkCar.getCreateTime());
        return parkCarDetailVo;
    }


    /**
     * 新增车辆前端实体转化为parkCar
     */
    public static ParkCar transformAddParkCar2ParkCar(ParkCarAddRequest request) {
        ParkCar parkCar = new ParkCar();
        parkCar.setParkId(request.getParkId());
        parkCar.setBeginDate(request.getBeginDate());
        parkCar.setParkUserId(request.getUserId());
        parkCar.setActualFee(null != request.getActualFee() ? request.getActualFee() : 0L);
        parkCar.setMayFee(null != request.getMayFee() ? request.getMayFee() : 0L);
        parkCar.setPayType(request.getPayType());
        parkCar.setEndDate(request.getEndDate());
        parkCar.setBindPlates(request.getPlates());
        parkCar.setPlate(request.getPlates().split(",")[0]);
        parkCar.setCarType("1");
        parkCar.setCreateUser(SecurityUtils.getUserId());
        parkCar.setIsGarageSpace(null != request.getIsGarageSpace() ? request.getIsGarageSpace() : "N");
        parkCar.setMngType(request.getMngType());
        parkCar.setMngTypeId(request.getMngTypeId());
        parkCar.setRemark(request.getRemark());
        parkCar.setSeatNo(request.getSeatNo());
        parkCar.setSpaces(request.getSpaceCnt());
        parkCar.setCreateTime(DateUtils.getNowDate());
        parkCar.setUpdateTime(DateUtils.getNowDate());
        parkCar.setStatus(request.getStatus());
        // 用户信息
        parkCar.setUserName(request.getUserName());
        parkCar.setPhone(request.getPhone());
        parkCar.setAddress(request.getAddress());
        parkCar.setCreateUser(SecurityUtils.getUserId() != null ? SecurityUtils.getUserId() : 1);
        parkCar.setUpdateUser(parkCar.getCreateUser());
        return parkCar;
    }

    /**
     * 编辑车辆前端实体转化为parkCar
     */
    public static ParkCar transformEditParkCar2ParkCar(ParkCarEditRequest request, ParkCar originalCar) {
        originalCar.setParkId(request.getParkId());
        originalCar.setParkUserId(request.getUserId());
        originalCar.setBindPlates(request.getPlates());
        originalCar.setPlate(request.getPlates().split(",")[0]);
        originalCar.setCarType("1");
        originalCar.setCreateUser(SecurityUtils.getUserId());
        originalCar.setMngType(request.getMngType());
        originalCar.setMngTypeId(request.getMngTypeId());
        originalCar.setRemark(request.getRemark());
        originalCar.setSeatNo(request.getSeatNo());
        originalCar.setSpaces(request.getSpaceCnt());
        originalCar.setStatus(request.getStatus());
        originalCar.setEndDate(request.getEndDate());
        originalCar.setBeginDate(request.getBeginDate());
        originalCar.setUserName(request.getUserName());
        originalCar.setPhone(request.getPhone());
        originalCar.setAddress(request.getAddress());
        originalCar.setUpdateUser(SecurityUtils.getUserId() != null ? SecurityUtils.getUserId() : 1);
        originalCar.setIsGarageSpace(request.getIsGarageSpace());
        return originalCar;
    }

    /**
     * 续费车辆前端实体转化为parkCar
     */
    public static ParkCar transformFeeParkCar2ParkCar(ParkCarFeeRequest request, ParkCar originalParkCar) {
        originalParkCar.setEndDate(request.getEndDate());
        originalParkCar.setMayFee(null != request.getFee() ? request.getFee().longValue() : 0L);
        originalParkCar.setActualFee(null != request.getActualFee() ? request.getActualFee().longValue() : 0L);
        return originalParkCar;
    }

    /**
     * 新增访客车辆前端实体转化为parkCar
     */
    public static ParkCar transformAddParkVisitorCar2ParkCar(ParkVisitorCarAddRequest request) {
        ParkCar parkCar = new ParkCar();
        parkCar.setParkId(request.getParkId());
        parkCar.setBeginDate(request.getBeginDate());
        parkCar.setEndDate(request.getEndDate());
        parkCar.setBindPlates(request.getPlates());
        parkCar.setPlate(request.getPlates().split(",")[0]);
        parkCar.setCarType("1");
        parkCar.setCreateUser(SecurityUtils.getUserId());
        parkCar.setMngType(request.getMngType());
        parkCar.setMngTypeId(request.getMngTypeId());
        parkCar.setRemark(request.getRemark());
        parkCar.setCreateTime(DateUtils.getNowDate());
        parkCar.setUpdateTime(DateUtils.getNowDate());
        parkCar.setStatus(request.getStatus());
        // 用户信息
        parkCar.setUserName(request.getName());
        parkCar.setPhone(request.getPhone());
        parkCar.setCreateUser(SecurityUtils.getUserId() != null ? SecurityUtils.getUserId() : 1);
        parkCar.setUpdateUser(parkCar.getCreateUser());
        return parkCar;
    }

    /**
     * 编辑访客车辆前端实体转化为parkCar
     */
    public static ParkCar transformEditVisitorParkCar2ParkCar(ParkVisitorCarAddRequest request, ParkCar originalCar) {
        originalCar.setParkId(request.getParkId());
        originalCar.setBindPlates(request.getPlates());
        originalCar.setPlate(request.getPlates().split(",")[0]);
        originalCar.setCarType("1");
        originalCar.setCreateUser(SecurityUtils.getUserId());
        originalCar.setMngType(request.getMngType());
        originalCar.setMngTypeId(request.getMngTypeId());
        originalCar.setRemark(request.getRemark());
        originalCar.setStatus(request.getStatus());
        originalCar.setEndDate(request.getEndDate());
        originalCar.setBeginDate(request.getBeginDate());
        originalCar.setUserName(request.getName());
        originalCar.setPhone(request.getPhone());
        originalCar.setUpdateUser(SecurityUtils.getUserId() != null ? SecurityUtils.getUserId() : 1);
        return originalCar;
    }
}
